package com.szh.taobaounion.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.szh.taobaounion.R;
import com.szh.taobaounion.model.domain.IBaseInfo;
import com.szh.taobaounion.model.domain.OnSellContent;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnSellContentAdapter extends RecyclerView.Adapter<OnSellContentAdapter.InnerHolder> {
    private List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData=new ArrayList<>();
    private OnSellClickListener mContentListener=null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_on_sell_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean = mData.get(position);
          holder.setData(mapDataBean);
          holder.itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (mContentListener != null) {
                      mContentListener.onSellItemClick(mapDataBean);
                  }
              }
          });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(OnSellContent result) {
        this.mData.clear();
         this.mData.addAll(result.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());
         notifyDataSetChanged();
    }

    /**
     * 加载更多
     * @param moreResult
     */
    public void onMoreLoaded(OnSellContent moreResult) {
        List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> moreData =
                moreResult.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        int oldSize=mData.size();
        this.mData.addAll(moreData);
        notifyItemChanged(oldSize-1,moreData.size());
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.on_sell_cover)
        public ImageView cover;

        @BindView(R.id.on_sell_content_title_tv)
        public TextView titleTv;

        @BindView(R.id.on_sell_origin_prise_tv)
        public TextView originalPriseTv;

        @BindView(R.id.on_sell_off_prise_tv)
        public TextView offPriseTv;
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
          titleTv.setText(mapDataBean.getTitle());
            String pict_url = mapDataBean.getPict_url();
            //LogUtils.d(this,"pict_url--->"+pict_url);
            String ticketUrl = UrlUtils.getTicketUrl(pict_url);
            Glide.with(cover.getContext()).load(ticketUrl).into(cover);
            String originalPrise = mapDataBean.getZk_final_price();
            originalPriseTv.setText("￥" + originalPrise + " ");
            originalPriseTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置中划线
            int couponAmount = mapDataBean.getCoupon_amount();
            float originPriseFloat = Float.parseFloat(originalPrise);
            float finalPrise = originPriseFloat - couponAmount;
            offPriseTv.setText("券后价：" + String.format("%.2f",finalPrise));
        }
    }
    public void setOnSellClickListener(OnSellClickListener onSellClickListener){
        this.mContentListener=onSellClickListener;
    }
    public interface  OnSellClickListener{
        void  onSellItemClick(IBaseInfo dataBean);
    }
}
